package com.nacos.alibaba.client.service.impl;

import com.github.pagehelper.PageHelper;
import com.nacos.alibaba.bo.AdminUserDetails;
import com.nacos.alibaba.client.service.UmsAdminService;
import com.nacos.alibaba.utils.JwtTokenUtil;
import com.nacos.alibaba.vo.UmsAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * UmsAdminService实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class UmsAdminServiceImpl implements UmsAdminService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Override
    public String refreshToken(String oldToken) {
        String token = oldToken.substring(tokenHead.length());
        if (jwtTokenUtil.canRefresh(token)) {
            return jwtTokenUtil.refreshToken(token);
        }
        return null;
    }

    @Override
    public AdminUserDetails getCurrentAdminUser() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        return (AdminUserDetails)auth.getPrincipal();
    }

    @Override
    public UmsAccount getAdminByUsername(String username) {
//        UmsAdminExample example = new UmsAdminExample();
//        example.createCriteria().andUsernameEqualTo(username);
//        List<UmsAdmin> adminList = adminMapper.selectByExample(example);

//        UmsAccountExample example = new UmsAccountExample();
//        example.createCriteria().andUserNameEqualTo(username);
//        List<UmsAccount> umsAccounts = umsAccountMapper.selectByExample(example);

//        if (umsAccounts != null && umsAccounts.size() > 0) {
//            return umsAccounts.get(0);
//        }
        return null;
    }
//
//    @Override
//    public UmsAdmin register(UmsAdminParam umsAdminParam) {
//        UmsAdmin umsAdmin = new UmsAdmin();
//        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
//        umsAdmin.setCreateTime(new Date());
//        umsAdmin.setStatus(1);
//        //查询是否有相同用户名的用户
//        UmsAdminExample example = new UmsAdminExample();
//        example.createCriteria().andUsernameEqualTo(umsAdmin.getUsername());
//        List<UmsAdmin> umsAdminList = adminMapper.selectByExample(example);
//        if (umsAdminList.size() > 0) {
//            return null;
//        }
//        //将密码进行加密操作
//        String encodePassword = passwordEncoder.encode(umsAdmin.getPassword());
//        umsAdmin.setPassword(encodePassword);
//        adminMapper.insert(umsAdmin);
//        return umsAdmin;
//    }
//
//    @Override
//    public String login(String username, String password) {
//        String token = null;
//        //密码需要客户端加密后传递
//        try {
//            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//            if(!passwordEncoder.matches(password,userDetails.getPassword())){
//                throw new BadCredentialsException("密码不正确");
//            }
//            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//            token = jwtTokenUtil.generateToken(userDetails);
////            updateLoginTimeByUsername(username);
////            insertLoginLog(username);
//        } catch (AuthenticationException e) {
//            LOGGER.warn("登录异常:{}", e.getMessage());
//        }
//        return token;
//    }
//
//    /**
//     * 添加登录记录
//     * @param username 用户名
//     */
////    private void insertLoginLog(String username) {
////        UmsAdmin admin = getAdminByUsername(username);
////        UmsAdminLoginLog loginLog = new UmsAdminLoginLog();
////        loginLog.setAdminId(admin.getId());
////        loginLog.setCreateTime(new Date());
////        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
////        HttpServletRequest request = attributes.getRequest();
////        loginLog.setIp(request.getRemoteAddr());
////        loginLogMapper.insert(loginLog);
////    }
//
//    /**
//     * 根据用户名修改登录时间
//     */
//    private void updateLoginTimeByUsername(String username) {
//        UmsAdmin record = new UmsAdmin();
//        record.setLoginTime(new Date());
//        UmsAdminExample example = new UmsAdminExample();
//        example.createCriteria().andUsernameEqualTo(username);
//        adminMapper.updateByExampleSelective(record, example);
//    }
//
//    @Override
//    public String refreshToken(String oldToken) {
//        String token = oldToken.substring(tokenHead.length());
//        if (jwtTokenUtil.canRefresh(token)) {
//            return jwtTokenUtil.refreshToken(token);
//        }
//        return null;
//    }
//
//    @Override
//    public UmsAdmin getItem(Long id) {
//        return adminMapper.selectByPrimaryKey(id);
//    }
//
//    @Override
//    public List<UmsAdmin> list(String name, Integer pageSize, Integer pageNum) {
//        PageHelper.startPage(pageNum, pageSize);
//        UmsAdminExample example = new UmsAdminExample();
//        UmsAdminExample.Criteria criteria = example.createCriteria();
//        if (!StringUtils.isEmpty(name)) {
//            criteria.andUsernameLike("%" + name + "%");
//            example.or(example.createCriteria().andNickNameLike("%" + name + "%"));
//        }
//        return adminMapper.selectByExample(example);
//    }
//
//    @Override
//    public int update(Long id, UmsAdmin admin) {
//        admin.setId(id);
//        //密码已经加密处理，需要单独修改
//        admin.setPassword(null);
//        return adminMapper.updateByPrimaryKeySelective(admin);
//    }
//
//    @Override
//    public int delete(Long id) {
//        return adminMapper.deleteByPrimaryKey(id);
//    }
//
//    @Override
//    public int updateRole(Long adminId, List<Long> roleIds) {
//        int count = roleIds == null ? 0 : roleIds.size();
//        //先删除原来的关系
//        UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();
//        adminRoleRelationExample.createCriteria().andAdminIdEqualTo(adminId);
//        adminRoleRelationMapper.deleteByExample(adminRoleRelationExample);
//        //建立新关系
//        if (!CollectionUtils.isEmpty(roleIds)) {
//            List<UmsAdminRoleRelation> list = new ArrayList<>();
//            for (Long roleId : roleIds) {
//                UmsAdminRoleRelation roleRelation = new UmsAdminRoleRelation();
//                roleRelation.setAdminId(adminId);
//                roleRelation.setRoleId(roleId);
//                list.add(roleRelation);
//            }
//            adminRoleRelationDao.insertList(list);
//        }
//        return count;
//    }
//
//    @Override
//    public List<UmsRole> getRoleList(Long adminId) {
//        return adminRoleRelationDao.getRoleList(adminId);
//    }
//
//    @Override
//    public int updatePermission(Long adminId, List<Long> permissionIds) {
//        //删除原所有权限关系
//        UmsAdminPermissionRelationExample relationExample = new UmsAdminPermissionRelationExample();
//        relationExample.createCriteria().andAdminIdEqualTo(adminId);
//        adminPermissionRelationMapper.deleteByExample(relationExample);
//        //获取用户所有角色权限
//        List<UmsPermission> permissionList = adminRoleRelationDao.getRolePermissionList(adminId);
//        List<Long> rolePermissionList = permissionList.stream().map(UmsPermission::getId).collect(Collectors.toList());
//        if (!CollectionUtils.isEmpty(permissionIds)) {
//            List<UmsAdminPermissionRelation> relationList = new ArrayList<>();
//            //筛选出+权限
//            List<Long> addPermissionIdList = permissionIds.stream().filter(permissionId -> !rolePermissionList.contains(permissionId)).collect(Collectors.toList());
//            //筛选出-权限
//            List<Long> subPermissionIdList = rolePermissionList.stream().filter(permissionId -> !permissionIds.contains(permissionId)).collect(Collectors.toList());
//            //插入+-权限关系
//            relationList.addAll(convert(adminId,1,addPermissionIdList));
//            relationList.addAll(convert(adminId,-1,subPermissionIdList));
//            return adminPermissionRelationDao.insertList(relationList);
//        }
//        return 0;
//    }
//
//    /**
//     * 将+-权限关系转化为对象
//     */
//    private List<UmsAdminPermissionRelation> convert(Long adminId,Integer type,List<Long> permissionIdList) {
//        List<UmsAdminPermissionRelation> relationList = permissionIdList.stream().map(permissionId -> {
//            UmsAdminPermissionRelation relation = new UmsAdminPermissionRelation();
//            relation.setAdminId(adminId);
//            relation.setType(type);
//            relation.setPermissionId(permissionId);
//            return relation;
//        }).collect(Collectors.toList());
//        return relationList;
//    }
//
//    @Override
//    public List<UmsPermission> getPermissionList(Long adminId) {
//        return adminRoleRelationDao.getPermissionList(adminId);
//    }
//
//    @Override
//    public UmsAccount getUserType(String shopUserName) {
//        UmsAccountExample umsAccountExample = new UmsAccountExample();
//        umsAccountExample.createCriteria().andUserNameEqualTo(shopUserName);
//        List<UmsAccount> umsAccounts = umsAccountMapper.selectByExample(umsAccountExample);
//        if(umsAccounts.size()>0){
//           return umsAccounts.get(0);
//        }
//        return null;
//    }
//
//    @Override
//    public AdminUserDetails getCurrentAdminUser() {
//        SecurityContext ctx = SecurityContextHolder.getContext();
//        Authentication auth = ctx.getAuthentication();
//        return (AdminUserDetails)auth.getPrincipal();
//    }
//    @Override
//    public List<ComShopInfo> getShopId(Integer accountId) {
//        ComShopInfoExample example = new ComShopInfoExample();
//        example.createCriteria().andUmsAccountIdEqualTo(accountId);
//        return comShopInfoMapper.selectByExample(example);
//    }
}
